#include "SkyBox.h"

namespace lib3dw
{
	SkyBox::SkyBox()
	{
		position = Vector3(0.0, 0.0, 0.0);
		textures = new GLuint[6];
		for (int loop = 0; loop < 6; loop++)
		{
			char* image = NULL;
			std::ostringstream si;
			si << loop;
			std::string filename = "data/SkyBox" + si.str() + ".png";
			result = LoadPng(filename.c_str(), &image, width, height);

			GLsizei w = (GLsizei)this->width;
			GLsizei h = (GLsizei)this->height;
			glGenTextures(1, &textures[loop]);
			glBindTexture(GL_TEXTURE_2D, textures[loop]);
			
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			
			glTexImage2D(GL_TEXTURE_2D, 0, 3, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
			free(image);
		}
	}

	SkyBox::~SkyBox()
	{
		delete[] textures;
	}
		
	GLuint* SkyBox::GetTexture(unsigned short index) const
	{
		return &textures[index];
	}
		
	void SkyBox::Draw()
	{
		Vector3 size = Vector3(40.0, 40.0, 40.0);

		glColor4f(1.0, 1.0, 1.0,1.0f);

		glPushMatrix();

		glDisable(GL_DEPTH_TEST);
		glEnable(GL_TEXTURE_2D);
		
		glTranslatef(position.x, position.y, position.z);
		glScalef(size.x, size.y, size.z);

		float cz = -0.0f,cx = 1.0f;
		float r = 1.0f;

		GLuint up = textures[0];
		GLuint down = textures[1];
		GLuint right = textures[2];
		GLuint left = textures[3];
		GLuint front = textures[4];
		GLuint back = textures[5];

		glBindTexture(GL_TEXTURE_2D, front);
		glBegin(GL_QUADS);	
		glTexCoord2f(cx, cz); glVertex3f(-r,-r, 1.0f);
		glTexCoord2f(cx, cx); glVertex3f(-r, r, 1.0f);
		glTexCoord2f(cz, cx); glVertex3f( r, r, 1.0f); 
		glTexCoord2f(cz, cz); glVertex3f( r,-r, 1.0f);
		glEnd();

		glBindTexture(GL_TEXTURE_2D, back);
		glBegin(GL_QUADS);
		glTexCoord2f(cx,cz);  glVertex3f(-r,-r,-1.0f);
		glTexCoord2f(cx,cx);  glVertex3f(-r, r,-1.0f);
		glTexCoord2f(cz,cx);  glVertex3f( r, r,-1.0f); 
		glTexCoord2f(cz,cz);  glVertex3f( r,-r,-1.0f);
		glEnd();

		glBindTexture(GL_TEXTURE_2D, left);			
		glBegin(GL_QUADS);
		glTexCoord2f(cx,cx); glVertex3f(-1.0f, r,-r);	
		glTexCoord2f(cz,cx); glVertex3f(-1.0f, r, r); 
		glTexCoord2f(cz,cz); glVertex3f(-1.0f,-r, r);
		glTexCoord2f(cx,cz); glVertex3f(-1.0f,-r,-r);		
		glEnd();

		glBindTexture(GL_TEXTURE_2D, right);	
		glBegin(GL_QUADS);
		glTexCoord2f( cx,cx); glVertex3f(1.0f, r,-r);	
		glTexCoord2f(cz, cx); glVertex3f(1.0f, r, r); 
		glTexCoord2f(cz, cz); glVertex3f(1.0f,-r, r);
		glTexCoord2f(cx, cz); glVertex3f(1.0f,-r,-r);
		glEnd();

		glBindTexture(GL_TEXTURE_2D, up);
		glBegin(GL_QUADS);
		glTexCoord2f(cz, cz); glVertex3f( r, 1.0f,-r);
		glTexCoord2f(cx, cz); glVertex3f( r, 1.0f, r); 
		glTexCoord2f(cx, cx); glVertex3f(-r, 1.0f, r);
		glTexCoord2f(cz, cx); glVertex3f(-r, 1.0f,-r);
		glEnd();

		glBindTexture(GL_TEXTURE_2D, down);	
		glBegin(GL_QUADS);
		glTexCoord2f(cz,cz);  glVertex3f( r,-1.0f,-r);
		glTexCoord2f( cx,cz); glVertex3f( r,-1.0f, r); 
		glTexCoord2f( cx,cx); glVertex3f(-r,-1.0f, r);
		glTexCoord2f(cz, cx); glVertex3f(-r,-1.0f,-r);
		glEnd();
		
		glDisable(GL_TEXTURE_2D);
		glEnable(GL_DEPTH_TEST);
		glPopMatrix();
	}
	
	void SkyBox::SetTranslation(Vector3 newTranslation)
	{
		position = newTranslation;
	}
}